package com.himalayan.bear.controller;

import com.alibaba.fastjson.JSONObject;
import com.himalayan.bear.common.util.StringUtils;
import com.himalayan.bear.entity.Files;
import com.himalayan.bear.entity.Folder;
import com.himalayan.bear.entity.User;
import com.himalayan.bear.entity.UserFile;
import com.himalayan.bear.service.FilesService;
import com.himalayan.bear.service.FolderService;
import com.himalayan.bear.service.UserFileService;
import com.himalayan.bear.util.FileUtil;
import com.himalayan.bear.util.RespUtil;
import com.himalayan.bear.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjk
 * @since 2019-04-25
 */
@Controller
@RequestMapping("/files")
public class FilesController extends BaseController {

    @Autowired
    protected FilesService filesService;

    @Autowired
    protected UserFileService userFileService;

    @Autowired
    protected FolderService folderService;

    @ResponseBody
    @RequestMapping("/rename")
    public JSONObject rename(String fileId, String fileName, String fType) {
        if (StringUtils.isEmpty(fileId) || StringUtils.isEmpty(fType)) {
            return RespUtil.FAIL;
        }
        String infoName = "0".equals(fType) ? "文件夹" : "文件";
        if (StringUtils.isEmpty(fileName)) {
            return RespUtil.msg(false, infoName + "名称不能为空 !");
        }
        if (!FileUtil.validFileName(fileName)) {
            return RespUtil.msg(false, "文件名不能包含以下字符<br>"
                    + Arrays.toString(FileUtil.NONAME));
        }
        if (fileName.length() > 100) {
            return RespUtil.msg(false, "文件名过长, 请控制在100字以内");
        }
        boolean r = false;
        switch (fType) {
            case "1": // 文件
                UserFile uf = new UserFile();
                uf.setId(fileId);
                uf.setOrgFileName(fileName);
                r = userFileService.updateById(uf);
                break;
            case "0": // 文件夹
                Folder folder = new Folder();
                folder.setId(fileId);
                folder.setFolderName(fileName);
                r = folderService.updateById(folder);
        }
        return RespUtil.msg(r);
    }

    @ResponseBody
    @RequestMapping("/saveFile")
    public JSONObject saveFile(String folderId, Files files, HttpSession session) {
        if (StringUtils.isEmpty(folderId)) {
            folderId = "0";
        }
        files.setId(UUIDUtil.getUUID());
        User user = getUser(session);
        files.setCreateName(user.getRealname());
        files.setCreateDate(LocalDateTime.now());
        files.setDeleteFlag(0);
        boolean r = filesService.save(files);
        if (r) {
            UserFile userFile = new UserFile();
            userFile.setId(UUIDUtil.getUUID());
            userFile.setUserId(user.getId());
            userFile.setFolderId(folderId);
            userFile.setFileId(files.getId());
            userFile.setOrgFileName(files.getFileName());
            userFile.setCode(files.getFileCode());
            userFile.setDeleteFlag(0);
            r = userFileService.save(userFile);
            return RespUtil.msg(r);
        }
        return RespUtil.FAIL;
    }
}
